package view;


import Dao.RalationalDataDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;

public class AddRltFra extends JFrame {
    Connection conn;
    String tableName;

    RelationFra rf;

    ArrayList<String> columnName=new ArrayList<>();
    ArrayList<String> columnType=new ArrayList<>();

    JTextField[] textFields;

    RalationalDataDao ralationalDataDao=new RalationalDataDao();


    JButton cancelBtn,OKBtn;
    AddRltFra(Connection conn,RelationFra rf,String tableName,ArrayList<String> columnName,ArrayList<String> columnType){
        //初始化变量
        this.conn=conn;
        this.tableName=tableName;
        this.columnName=columnName;
        this.rf=rf;//父窗口
        this.columnType=columnType;
        textFields=new JTextField[columnName.size()];
        //初始化窗口
        setBounds(300,100,400,400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(columnName.size()+1,2));
        addContent();
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        //监听
        addListener();
    }

    private void addListener() {
        //关闭窗口
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        //确认添加
        OKBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addActionPerformed();
                rf.fillTable();//刷新表格
            }
        });
    }

    //添加信息响应函数
    private void addActionPerformed() {
        ArrayList<String> data=new ArrayList<>();
        //获取输入信息
        for (int i = 0; i < textFields.length; i++) {
            data.add(textFields[i].getText());
        }

        try {
            //添加
            int num=ralationalDataDao.AddData(conn,tableName,columnName,columnType,data);
            if(num<1){
                JOptionPane.showMessageDialog(null,"添加失败");
            }
            else {
                JOptionPane.showMessageDialog(null,"添加成功");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"添加失败");
            throw new RuntimeException(e);
        }
    }

    //添加窗口控件
    private void addContent() {
        int i=0;
        //添加每个字段的输入框
        for (String s : columnName) {
            JLabel label = new JLabel(s);
            label.setPreferredSize(new Dimension(100, 50));
            textFields[i]=new JTextField();
            textFields[i].setPreferredSize(new Dimension(200, 50));
            getContentPane().add(label);
            getContentPane().add(textFields[i]);
            i++;
        }
        //按钮
        cancelBtn=new JButton("取消");
        OKBtn=new JButton("添加");
        cancelBtn.setPreferredSize(new Dimension(50,20));
        OKBtn.setPreferredSize(new Dimension(50,20));
        getContentPane().add(OKBtn);
        getContentPane().add(cancelBtn);
    }
}
